/**
 * 本地存储工具类
 * 用于保存和读取游戏数据
 */
class Storage {
    constructor() {
        // 存储键名前缀
        this.keyPrefix = 'keyboard_thief_';
        
        // 检查localStorage是否可用
        this.isAvailable = this.checkAvailability();
    }
    
    /**
     * 检查localStorage是否可用
     * @returns {boolean} 是否可用
     */
    checkAvailability() {
        try {
            const testKey = '__test__';
            localStorage.setItem(testKey, testKey);
            localStorage.removeItem(testKey);
            return true;
        } catch (e) {
            console.warn('localStorage不可用，游戏数据将不会被保存');
            return false;
        }
    }
    
    /**
     * 保存高分记录
     * @param {string} difficulty 难度
     * @param {number} score 得分
     * @param {Object} stats 游戏统计数据
     */
    saveScore(difficulty, score, stats) {
        if (!this.isAvailable) return;
        
        const key = `${this.keyPrefix}highscore_${difficulty}`;
        
        // 获取现有高分
        const existingScores = this.getHighScores(difficulty);
        
        // 添加新分数
        const newScore = {
            score: score,
            date: new Date().toISOString(),
            stats: stats
        };
        
        existingScores.push(newScore);
        
        // 按分数降序排序
        existingScores.sort((a, b) => b.score - a.score);
        
        // 只保留前10个高分
        const topScores = existingScores.slice(0, 10);
        
        // 保存到localStorage
        localStorage.setItem(key, JSON.stringify(topScores));
    }
    
    /**
     * 获取高分记录
     * @param {string} difficulty 难度
     * @returns {Array} 高分记录数组
     */
    getHighScores(difficulty) {
        if (!this.isAvailable) return [];
        
        const key = `${this.keyPrefix}highscore_${difficulty}`;
        const scoresJson = localStorage.getItem(key);
        
        if (!scoresJson) {
            return [];
        }
        
        try {
            return JSON.parse(scoresJson);
        } catch (e) {
            console.error('解析高分记录失败', e);
            return [];
        }
    }
    
    /**
     * 保存游戏设置
     * @param {Object} settings 游戏设置
     */
    saveSettings(settings) {
        if (!this.isAvailable) return;
        
        const key = `${this.keyPrefix}settings`;
        localStorage.setItem(key, JSON.stringify(settings));
    }
    
    /**
     * 获取游戏设置
     * @returns {Object} 游戏设置
     */
    getSettings() {
        if (!this.isAvailable) return {};
        
        const key = `${this.keyPrefix}settings`;
        const settingsJson = localStorage.getItem(key);
        
        if (!settingsJson) {
            return {};
        }
        
        try {
            return JSON.parse(settingsJson);
        } catch (e) {
            console.error('解析游戏设置失败', e);
            return {};
        }
    }
    
    /**
     * 清除所有游戏数据
     */
    clearAll() {
        if (!this.isAvailable) return;
        
        // 获取所有键
        const keys = [];
        for (let i = 0; i < localStorage.length; i++) {
            const key = localStorage.key(i);
            if (key.startsWith(this.keyPrefix)) {
                keys.push(key);
            }
        }
        
        // 删除所有相关键
        keys.forEach(key => localStorage.removeItem(key));
    }
} 